package com.example.dao;

import com.example.model.LabReservation;
import com.example.utils.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class LabReservationDAO {
    // 根据用户ID查询个人预约
    public List<LabReservation> findAllReservationsByUserId(long userId) {
        List<LabReservation> reservations = new ArrayList<>();
        String sql = "SELECT * FROM lab_reservation WHERE user_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setLong(1, userId);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                LabReservation reservation = new LabReservation();
                reservation.setReservationId(rs.getLong("reservation_id"));
                reservation.setLabId(rs.getLong("lab_id"));
                reservation.setUserId(rs.getLong("user_id"));
                reservation.setReservationDate(rs.getString("reservation_date"));
                reservation.setStartTime(rs.getString("start_time"));
                reservation.setEndTime(rs.getString("end_time"));
                reservation.setPurpose(rs.getString("purpose"));
                reservation.setStatus(rs.getString("status"));
                reservations.add(reservation);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return reservations;
    }

    // 查询所有预约（管理员使用）
    public List<LabReservation> findAllReservations() {
        List<LabReservation> reservations = new ArrayList<>();
        String sql = "SELECT * FROM lab_reservation";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                LabReservation reservation = new LabReservation();
                reservation.setReservationId(rs.getLong("reservation_id"));
                reservation.setLabId(rs.getLong("lab_id"));
                reservation.setUserId(rs.getLong("user_id"));
                reservation.setReservationDate(rs.getString("reservation_date"));
                reservation.setStartTime(rs.getString("start_time"));
                reservation.setEndTime(rs.getString("end_time"));
                reservation.setPurpose(rs.getString("purpose"));
                reservation.setStatus(rs.getString("status"));
                reservations.add(reservation);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return reservations;
    }

    // 搜索表预约
    public List<LabReservation> searchReservations(String keyword) {
        List<LabReservation> reservations = new ArrayList<>();
        String sql = "SELECT * FROM lab_reservation WHERE purpose LIKE ? ESCAPE '!' OR reservation_date LIKE ? ESCAPE '!'";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, "%" + keyword + "%");
            pstmt.setString(2, "%" + keyword + "%");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                LabReservation reservation = new LabReservation();
                reservation.setReservationId(rs.getLong("reservation_id"));
                reservation.setLabId(rs.getLong("lab_id"));
                reservation.setUserId(rs.getLong("user_id"));
                reservation.setReservationDate(rs.getString("reservation_date"));
                reservation.setStartTime(rs.getString("start_time"));
                reservation.setEndTime(rs.getString("end_time"));
                reservation.setPurpose(rs.getString("purpose"));
                reservation.setStatus(rs.getString("status"));
                reservations.add(reservation);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return reservations;
    }

    // 添加预约
    public boolean addReservation(LabReservation reservation) {
        String sql = "INSERT INTO lab_reservation (lab_id, user_id, reservation_date, start_time, end_time, purpose) VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setLong(1, reservation.getLabId());
            pstmt.setLong(2, reservation.getUserId());
            pstmt.setString(3, reservation.getReservationDate());
            pstmt.setString(4, reservation.getStartTime());
            pstmt.setString(5, reservation.getEndTime());
            pstmt.setString(6, reservation.getPurpose());
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}